import { fetchExam, createExam, subject, examType, updateExam, fetchDetail, deleteExam } from '../services/exam'
import { message } from "antd";

export default {

    namespace: 'exam',

    state: {
        subject: [], // 所有的课程
        examType: [], // 考试类型
        examList: {}, //获取试卷列表
    },

    subscriptions: {
        setup({ dispatch, history }) {  // eslint-disable-line
            dispatch({
                type: 'listExam'
            })
        },
    },

    effects: {
        //获取试卷列表
        *listExam({ payload }, { call, put }) {
            const promiseAll = [
                subject(),
                examType(),
            ]
            const initData = () => Promise.all(promiseAll)
            const res = yield call(initData)
            // console.log(res)
            yield put({
                type: 'changeState',
                payload: {
                    subject: res[0].data,
                    examType: res[1].data, 
                }
            })
            yield put({
                type: 'fetchExam'
            })
        },
        //获取试卷
        *fetchExam({ payload }, { call, put }) {
            let res = yield call(fetchExam, payload)
            yield put({
                type: 'changeState',
                payload: {
                    examList: res
                }
            })
        },
        //创建试卷
        *createExam({ payload }, { call, put, select }) {
            const res = yield call(createExam, payload)
            message.error(res.msg)
        },
        *updateExam({ payload }, { call, put, select }){
            const res = yield call(createExam, payload)
            return Promise.resolve(res)
           
        },
        *fetch({ payload }, { call, put, select }) {  // eslint-disable-line
            yield put({ type: 'save' });
        },
    },

    reducers: {
        changeState(state, action) {
            return { ...state, ...action.payload };
        },
    },

};